﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using System.Data;
using System.Collections;
using EntitySpaces.Interfaces;

public partial class Admin_NoSAPFin_NoSAPFin002 : AdminBasePage
{
    protected decimal? boxId;
    protected string receiptCount = "0";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["BoxId"] != null)
        {
            boxId = decimal.Parse(Request.QueryString["BoxId"]);
        }

        if (!IsPostBack)
        {

            if (Session["sd"] != null)
            {
                txtStartDate.Value = Session["sd"].ToString();
            }
            if (Session["ed"] != null)
            {
                txtEndate.Value = Session["ed"].ToString();
            }
            if (Session["txtBoxNo"] != null)
            { txtBoxNo.Text = Session["txtBoxNo"].ToString(); }
            if (Session["txtSupplierBoxNo"] != null)
            { txtSupplierBoxNo.Text = Session["txtSupplierBoxNo"].ToString(); }
            if (Session["drpVoucherDescription"] != null)
            {
                drpVoucherDescription.SelectedValue = Session["drpVoucherDescription"].ToString();
            }
            if (Session["txtStoreStartDate"] != null)
            {
                txtStoreStartDate.Value = Session["txtStoreStartDate"].ToString();
            }
            if (Session["txtStoreEndDate"] != null)
            {
                txtStoreEndDate.Value = Session["txtStoreEndDate"].ToString();
            }
            if (Session["txtKeyStart"] != null)
            { txtKeyStart.Text = Session["txtKeyStart"].ToString(); }
            if (Session["txtKeyEnd"] != null)
            { txtKeyEnd.Text = Session["txtKeyEnd"].ToString(); }
            if (Session["drpCompany"] != null)
            {
                drpCompany.SelectedValue = Session["drpCompany"].ToString();
            }
            if (Session["drpFunctionDept"] != null)
            {
                drpFunctionDept.SelectedValue = Session["drpFunctionDept"].ToString();
            }
            if (Session["drpManageUser"] != null)
            {
                drpManageUser.SelectedValue = Session["drpManageUser"].ToString();
            }
            if (Session["HisDocumentStartDate"] != null)
            {
                switch (Session["HisDocumentStartDate"].ToString().Length)
                {
                    case 4:
                        drpHisDocumentYearStart.SelectedValue = Session["HisDocumentStartDate"].ToString();
                        break;
                    case 6:
                        drpHisDocumentYearStart.SelectedValue = Session["HisDocumentStartDate"].ToString().Substring(0,4);
                        drpHisDocumentMonthStart.SelectedValue = Session["HisDocumentStartDate"].ToString().Substring(4,2);
                        break;
                    case 8:
                        drpHisDocumentYearStart.SelectedValue = Session["HisDocumentStartDate"].ToString().Substring(0,4);
                        drpHisDocumentMonthStart.SelectedValue = Session["HisDocumentStartDate"].ToString().Substring(4,2);
                        drpHisDocumentDayStart.SelectedValue = Session["HisDocumentStartDate"].ToString().Substring(6,2);
                        break;
                }
            }

            if (Session["HisDocumentEndDate"] != null)
            {
                switch (Session["HisDocumentEndDate"].ToString().Length)
                {
                    case 4:
                        drpHisDocumentYearEnd.SelectedValue = Session["HisDocumentEndDate"].ToString();
                        break;
                    case 6:
                        drpHisDocumentYearEnd.SelectedValue = Session["HisDocumentEndDate"].ToString().Substring(0, 4);
                        drpHisDocumentMonthEnd.SelectedValue = Session["HisDocumentEndDate"].ToString().Substring(4, 2);
                        break;
                    case 8:
                        drpHisDocumentYearEnd.SelectedValue = Session["HisDocumentEndDate"].ToString().Substring(0, 4);
                        drpHisDocumentMonthEnd.SelectedValue = Session["HisDocumentEndDate"].ToString().Substring(4, 2);
                        drpHisDocumentDayEnd.SelectedValue = Session["HisDocumentEndDate"].ToString().Substring(6, 2);
                        break;
                }
            }
            if (Session["drpIsDelete"] != null)
            {
                drpIsDelete.SelectedValue = Session["drpIsDelete"].ToString();
            }
        }
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["txtBoxNo"] = txtBoxNo.Text.Trim();
        Session["txtSupplierBoxNo"] = txtSupplierBoxNo.Text.Trim();
        Session["drpVoucherDescription"] = drpVoucherDescription.SelectedValue;
        Session["txtStoreStartDate"] = txtStoreStartDate.Value;
        Session["txtStoreEndDate"] = txtStoreEndDate.Value;
        Session["HisDocumentStartDate"] = drpHisDocumentYearStart.SelectedValue + drpHisDocumentMonthStart.SelectedValue + drpHisDocumentDayStart.SelectedValue;
        Session["HisDocumentEndDate"] = drpHisDocumentYearEnd.SelectedValue + drpHisDocumentMonthEnd.SelectedValue + drpHisDocumentDayEnd.SelectedValue;
        Session["txtKeyStart"] = txtKeyStart.Text.Trim();
        Session["txtKeyEnd"] = txtKeyEnd.Text.Trim();
        Session["drpCompany"] = drpCompany.SelectedValue;
        Session["drpFunctionDept"] = drpFunctionDept.SelectedValue;
        Session["sd"] = txtStartDate.Value;
        Session["ed"] = txtEndate.Value;
        Session["drpManageUser"] = drpManageUser.SelectedValue;
        Session["drpIsDelete"] = drpIsDelete.SelectedValue;
    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        FinReceiptQuery q1 = new FinReceiptQuery("q1");
        FinBoxQuery q2 = new FinBoxQuery("q2");
        FinBoxReceiptsQuery q3 = new FinBoxReceiptsQuery("q3");

        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");
        TSCodeTableQuery q5 = new TSCodeTableQuery("q5");
        TSCodeTableQuery q6 = new TSCodeTableQuery("q6");
        TSCodeTableQuery q7 = new TSCodeTableQuery("q7");
        BFThirdUnitQuery q8 = new BFThirdUnitQuery("q8");

        q1.InnerJoin(q2).On(q1.BoxID==q2.BoxID);
        q1.InnerJoin(q3).On(q1.ReceiptID == q3.ReceiptID);
        q1.LeftJoin(q4).On(q1.FunctionDept == q4.Code);
        q1.LeftJoin(q5).On(q1.StorageStatus == q5.Code);
        q1.LeftJoin(q6).On(q1.VoucherDescription == q6.Code);
        q1.LeftJoin(q7).On(q1.EStorageStatus == q7.Code);
        q1.LeftJoin(q8).On(q1.StorageLocation == q8.ThirdUnitID);


        q1.Select(q1.ReceiptID,q2.BoxID, q2.BoxNo,q2.SupplierBoxNo, q4.Value1.As("FunctionDeptName"), q5.Value1.As("StorageStatusName"), q6.Value1.As("VoucherDescriptionName"), q8.Name, q8.CartName
            , q7.Value1.As("EStorageStatusName"),q1.FirstStorageDate,q1.CreatedBy,q1.HisDocumentEndDate,q1.HisDocumentStartDate,q1.HisIndexEnd,q1.HisIndexStart,q2.NoSapReceiptCount,q2.CompanyCode,q2.CompanyName
           , q1.StoreStartDate, q1.NeedStoreYear, q1.DestoryDate, q1.Comments,q1.EStorageLocation,q1.IsDeleted,q1.ManageUser
            );

        ArrayList args = new ArrayList();
        ArrayList argsOr = new ArrayList();
        args.Add(q1.IsSAP==false);
        if (!string.IsNullOrEmpty(txtBoxNo.Text.Trim()))
        {
            args.Add(q2.BoxNo.Like("%" + txtBoxNo.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(txtSupplierBoxNo.Text.Trim()))
        {
            args.Add(q2.SupplierBoxNo.Like("%" + txtSupplierBoxNo.Text.Trim() + "%"));
        }
        if (!string.IsNullOrEmpty(drpVoucherDescription.SelectedValue))
        {
            args.Add(q1.VoucherDescription == drpVoucherDescription.SelectedValue);
        }

        if (!string.IsNullOrEmpty(drpCompany.SelectedValue))
        {
            string[] companyInfo = drpCompany.SelectedValue.Split('/');
            args.Add(q1.CompanyName == companyInfo[0]);
            args.Add(q1.CompanyCode == companyInfo[1]);
        }

        if (!string.IsNullOrEmpty(txtStoreStartDate.Value))
        {
            args.Add(q1.FirstStorageDate >= txtStoreStartDate.Value);
        }
        if (!string.IsNullOrEmpty(txtStoreEndDate.Value))
        {
            args.Add(q1.FirstStorageDate <= DateTime.Parse(txtStoreEndDate.Value).AddDays(1));
        }

        if (Session["HisDocumentStartDate"] != null)
        {
            args.Add(q1.HisDocumentStartDate <= Session["HisDocumentStartDate"].ToString());
            args.Add(q1.HisDocumentEndDate >= Session["HisDocumentStartDate"].ToString());
        }

        if (Session["HisDocumentEndDate"] != null)
        {
            args.Add(q1.HisDocumentStartDate <= Session["HisDocumentEndDate"].ToString());
            args.Add(q1.HisDocumentEndDate >= Session["HisDocumentEndDate"].ToString());
        }

        if (!string.IsNullOrEmpty(txtKeyStart.Text.Trim()))
        {
            args.Add(q1.HisIndexStart <= txtKeyStart.Text.Trim());
            args.Add(q1.HisIndexEnd >= txtKeyStart.Text.Trim());
        }

        if (!string.IsNullOrEmpty(txtKeyEnd.Text.Trim()))
        {
            args.Add(q1.HisIndexStart <= txtKeyEnd.Text.Trim());
            args.Add(q1.HisIndexEnd >= txtKeyEnd.Text.Trim());
        }

        if (!string.IsNullOrEmpty(drpFunctionDept.SelectedValue))
        {
            args.Add(q1.FunctionDept == drpFunctionDept.SelectedValue);
        }
        if (!string.IsNullOrEmpty(drpManageUser.SelectedValue))
        {
            args.Add(q1.ManageUser == drpManageUser.SelectedValue);
        }
        if (!string.IsNullOrEmpty(txtStartDate.Value))
        {
            args.Add(q1.CreatedDttm >= txtStartDate.Value);
        }
        if (!string.IsNullOrEmpty(txtEndate.Value))
        {
            args.Add(q1.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));
        }
        if (!string.IsNullOrEmpty(drpIsDelete.SelectedValue))
        {
            if (drpIsDelete.SelectedValue == "1")
            { args.Add(q1.IsDeleted == true); }
            if (drpIsDelete.SelectedValue == "0")
            { args.Add(q1.IsDeleted == false); }
        }

        q1.Where(args.ToArray()).OrderBy(q2.BoxNo.Ascending);

        DataTable tableSorce = q1.LoadDataTable();
        pageGrid.DataSource = tableSorce;
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        Session.Remove("txtBoxNo");
        Session.Remove("txtSupplierBoxNo");
        Session.Remove("drpVoucherDescription");
        Session.Remove("txtStoreStartDate");
        Session.Remove("txtStoreEndDate");
        Session.Remove("HisDocumentStartDate");
        Session.Remove("HisDocumentEndDate");
        Session.Remove("txtKeyStart");
        Session.Remove("txtKeyEnd");
        Session.Remove("drpCompany");
        Session.Remove("drpFunctionDept");
        Session.Remove("sd");
        Session.Remove("ed");
        Session.Remove("drpManageUser");
    }


    #region  Delete_Command
    protected void Delete_Command(object sender, CommandEventArgs e)
    {
        decimal Id = decimal.Parse(e.CommandArgument.ToString());
        using (esTransactionScope tr = new esTransactionScope())
        {
            FinReceipt finReceipt = new FinReceipt();
            FinBox finBox = new FinBox();
            FinBoxReceipts finBoxReceipts = new FinBoxReceipts();

            finReceipt.LoadByPrimaryKey(Id);
            finReceipt.IsDeleted = true;
            decimal? boxId = finReceipt.BoxID;
            if (boxId.HasValue)
            {
                finBox.LoadByPrimaryKey(boxId.Value);
                finBox.IsDeleted = true;
            }
            FinBoxReceiptsCollection finBoxReceiptsCollection = new FinBoxReceiptsCollection();
            finBoxReceiptsCollection.Query.Where(finBoxReceiptsCollection.Query.BoxID == finBox.BoxID, finBoxReceiptsCollection.Query.ReceiptID == finReceipt.ReceiptID);
            finBoxReceiptsCollection.Query.Load();
            if (finBoxReceiptsCollection.HasData)
            {
                finBoxReceipts.LoadByPrimaryKey(finBoxReceiptsCollection[0].BoxReceiptID.Value);
                finBoxReceipts.IsDeleted = true;
            }

            finBoxReceipts.Save();
            finReceipt.Save();
            finBox.Save();
            tr.Complete();
        }

        ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showMsg()", true);
        
    }
    #endregion

}